import request from '@renderer/utils/request';
import { useConfigStore } from '@renderer/stores/configStore'

// 辅助函数：延迟获取基础 URL（确保在应用挂载后执行）
const getBaseUrl = () => {
  const configStore = useConfigStore();
  return configStore.baseUrl;
};

export function tagApi() {
    return {
      getTags: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/get_tags',
          method: 'get',
          params: data,
        })
      },
      getCategories: () => {
        return request({
          url: getBaseUrl() + 'sql/get_categories',
          method: 'get',
        })
      },
      updateTag: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/update_tag',
          method: 'post',
          data: data,
        })
      },
      addTag: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/add_tag',
          method: 'post',
          data: data,
        })
      },
      deleteTag: (data: object) => {
        return request({
          url: `${getBaseUrl()}sql/delete_tag`,
          method: 'post',
          data: data
        })
      },
      deleteTagsByFilter: (params: object) => {
        return request({
          url: getBaseUrl() + 'sql/delete_tags_by_filter',
          method: 'delete',
          params: params,
        })
      },
      getRandomTagsConfig: () => {
        return request({
          url: getBaseUrl() + 'sql/get_random_configs',
          method: 'get',
        })
      },
      getRandomTagsConfigById: (configId) => {
        return request({
          url: getBaseUrl() + `sql/get_random_config/${configId}`,
          method: 'get'
        });
      },
      batchGenerateTags: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/batch_random_tags',
          method: 'post',
          data: data,
        })
      },
      batchGenerateSequentialTags: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/sequential_tags',
          method: 'post',
          data: data
        })
      },
      saveRandomConfig: (data: object) => {
        return request({
          url: getBaseUrl() + 'sql/save_random_config',
          method: 'post',
          data: data
        })
      },
      deleteRandomConfig: (configId) => {
        return request({
          url: getBaseUrl() + `sql/delete_random_config/${configId}`,
          method: 'delete'
        });
      },
      exportTagStream: (params: any) => {
        return request({
          url: getBaseUrl() + 'sql/export_tags_stream',
          method: 'get',
          params: params,
          responseType: 'blob',
        });
      },
      importTagCsv: (data: any) => {
        return request({
          url: getBaseUrl() + 'import/tags-csv',
          method: 'post',
          data: data,
          headers: { 'Content-Type': undefined }
        });
      },
    }
}
    